﻿using System;
using System.Collections.Generic;
using System.Data;
using Pku.BookStore.Model;

namespace Pku.BookStore.DAL
{
    /// <summary>
    /// 数据访问类
    /// </summary>
    public class OperateLogDAL : BaseDAL
    {
        string commandText = string.Empty;

        /// <summary>
        /// 增加日志
        /// </summary>
        /// <param name="entity"></param>
        public void Add(OperateLogModel entity)
        {
            commandText = @"INSERT INTO OperateLog(Operator,OperateType,Content,OpreateDate)
VALUES(@Operator,@OperateType,@Content,getdate());
SET  @LogID=scope_identity();
";
            var command = DbMain.GetSqlStringCommand(commandText);
            DbMain.AddInParameter(command, "LogID", DbType.Int32, entity.LogID);
            DbMain.AddInParameter(command, "Operator", DbType.Int32, entity.Operator);
            DbMain.AddInParameter(command, "OperateType", DbType.Int32, entity.OperateType);
            DbMain.AddInParameter(command, "Content", DbType.String, entity.Content);

            DbMain.ExecuteNonQuery(command);

            entity.LogID = (int)DbMain.GetParameterValue(command, "LogID");

        }

        /// <summary>
        /// 查询日志
        /// </summary>
        /// <returns></returns>
        public List<OperateLogModel> Search(OperateLogParam param)
        {
            List<OperateLogModel> list = new List<OperateLogModel>();
            commandText = @"SELECT LogID,Operator,OperateType,Content,OpreateDate  FROM  OperateLog";
            using (var dr = DbMain.ExecuteReader(CommandType.Text, commandText))
            {
                while (dr.Read())
                {
                    list.Add(ConvertReaderToEntity(dr));
                }
            }
            return list;
        }

        /// <summary>
        /// 获取实体
        /// </summary>
        /// <param name="logID"></param>
        /// <returns></returns>
        public OperateLogModel GetEntityById(int logID)
        {
            commandText = @"SELECT LogID,Operator,OperateType,Content,OpreateDate  FROM  OperateLog WHERE LogID=@LogID";
            var command = DbMain.GetSqlStringCommand(commandText);
            DbMain.AddInParameter(command, "LogID", DbType.Int32, logID);
            using (var dr = DbMain.ExecuteReader(command))
            {
                if (dr.Read())
                {
                    return ConvertReaderToEntity(dr);
                }
            }
            return null;
        }

        /// <summary>
        /// 转换实体
        /// </summary>
        /// <param name="dr"></param>
        /// <returns></returns>
        private static OperateLogModel ConvertReaderToEntity(IDataRecord dr)
        {
            return new OperateLogModel
            {
                LogID = (int)dr["LogID"],
                Operator = (int)dr["Operator"],
                OperateType = (int)dr["OperateType"],
                Content = dr["Content"].ToString(),
                OpreateDate = Convert.ToDateTime(dr["OpreateDate"])
            };
        }
    }

}